/*******************************************************************************
 * Copyright (c) 2016, 2025 Lablicate GmbH.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 * Philip Wenig - initial API and implementation
 *******************************************************************************/
package org.eclipse.chemclipse.model.selection;

import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IScan;

public class ChromatogramSelectionSupport {

	private ChromatogramSelectionSupport() {

	}

	public static void moveRetentionTimeWindow(IChromatogramSelection chromatogramSelection, MoveDirection moveDirection, int retentionTimeDivider) {

		int startRetentionTime = chromatogramSelection.getStartRetentionTime();
		int stopRetentionTime = chromatogramSelection.getStopRetentionTime();
		int retentionTimeMoveWindow = (stopRetentionTime - startRetentionTime) / retentionTimeDivider;

		int startRetentionTimeNew = (MoveDirection.LEFT.equals(moveDirection)) ? startRetentionTime - retentionTimeMoveWindow : startRetentionTime + retentionTimeMoveWindow;
		int stopRetentionTimeNew = (MoveDirection.LEFT.equals(moveDirection)) ? stopRetentionTime - retentionTimeMoveWindow : stopRetentionTime + retentionTimeMoveWindow;

		startRetentionTimeNew = getValidatedStartRetentionTime(chromatogramSelection, startRetentionTimeNew);
		stopRetentionTimeNew = getValidatedStopRetentionTime(chromatogramSelection, stopRetentionTimeNew);
		chromatogramSelection.setRanges(startRetentionTimeNew, stopRetentionTimeNew, chromatogramSelection.getStartAbundance(), chromatogramSelection.getStopAbundance());
	}

	public static int getValidatedStartRetentionTime(IChromatogramSelection chromatogramSelection, int startRetentionTimeNew) {

		IChromatogram chromatogram = chromatogramSelection.getChromatogram();
		int minRetentionTime = chromatogram.getStartRetentionTime();
		if(startRetentionTimeNew < minRetentionTime) {
			return minRetentionTime;
		} else {
			return startRetentionTimeNew;
		}
	}

	public static int getValidatedStopRetentionTime(IChromatogramSelection chromatogramSelection, int stopRetentionTimeNew) {

		IChromatogram chromatogram = chromatogramSelection.getChromatogram();
		int maxRetentionTime = chromatogram.getStopRetentionTime();
		if(stopRetentionTimeNew > maxRetentionTime) {
			return maxRetentionTime;
		} else {
			return stopRetentionTimeNew;
		}
	}

	public static boolean containsEmptyScans(IChromatogramSelection chromatogramSelection) {

		IChromatogram chromatogram = chromatogramSelection.getChromatogram();
		int retentionTimeStart = chromatogramSelection.getStartRetentionTime();
		int retentionTimeStop = chromatogramSelection.getStopRetentionTime();
		for(IScan scan : chromatogram.getScans()) {
			int retentionTime = scan.getRetentionTime();
			if(retentionTime >= retentionTimeStart && retentionTime <= retentionTimeStop) {
				if(scan.getTotalSignal() == 0) {
					return true;
				}
			}
		}
		return false;
	}
}
